<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        span{
            display:block;
            width: 200px;
            height: 50px;
            background-color: #888888;
            user-select: none;
         
        }
        #sub{
            margin-left: 10px;
            float: left;
            margin-top: 10px;
        }
        #yzm{
            float: left;
            margin-top: 10px;
        }
        #txt{
            float: left;
            margin-top: 10px;
        }
    </style>
</head>
<body>
    
    <input type="text" id="txt">
    <span id="yzm"></span></br>
    <input type="submit" name="inp" id="sub">
    <script>

        // 初始验证码
        
        yzm.innerHTML = parseInt(Math.random()*10000)
        // 点击更换验证码
        yzm.onclick = function(){
            yzm.innerHTML = parseInt(Math.random()*10000)
        }
        // 匹配判断
        sub.onclick = function(){
            if(txt.value === yzm.innerHTML){
                alert('提交成功')
            }else{
                alert('验证码错误')
            }
        }

        
       
        
        
    </script>
</body>
</html>